//
//  DutyViewController.m
//  Nurse
//
//  Created by Sunny on 13-12-13.
//  Copyright (c) 2013年 RainSets. All rights reserved.
//

#import "NursingdocumentsViewController.h"
#import "PatientInfoView.h"
#import "SignsViewCell.h"
#import "PatientService.h"

@interface NursingdocumentsViewController ()

@end

@implementation NursingdocumentsViewController
//@synthesize data;
//@synthesize treeView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title  = @"护理文书";
   [self setleftBarButtonItem:@""orImaged:@"head-icon_04.png" Event:@selector(leftBar_clicked:)];
    
    [self setrightBarButtonItem:@""orImaged:@"head-icon_02.png" Event:@selector(rightBar_clicked:)];
    
    
  //  NSArray *ibs = [[NSBundle mainBundle] loadNibNamed:@"PatientInfoView" owner:nil options:nil];
    // TestView *test = [ibs objectAtIndex:0];
//    PatientInfoView *pInfoView = (PatientInfoView *)[ibs objectAtIndex:0];
//    [self.view addSubview:pInfoView];
    
    //#import "PatientService.h"
    NSDictionary* patient = [[PatientService sharedInstance]currentPatient];
    
    self.pInfoView.txtPatientName.text = [patient valueForKey:@"姓名"];
    self.pInfoView.txtPatientID.text = [NSString stringWithFormat:@"住院号:%@",[patient valueForKey:@"住院号"]];
    self.pInfoView.txtBedNo.text = [NSString stringWithFormat:@"床位号:%@",[patient valueForKey:@"床位号"]];
    NSString*xb= [patient valueForKey:@"性别"];
    if ([xb isEqualToString:@"女"]) {
        self.pInfoView.imgxb.image=[UIImage imageNamed:@"gender-female2.png"];
        
    }
    else{
        self.pInfoView.imgxb.image=[UIImage imageNamed:@"gender-male2.png"];
    }
    
    _dataList = [[NSMutableArray alloc] init];
    
    for (int i=0; i<10; i++) {
        
        NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
        [dic setObject:@"2013-12-25" forKey:@"name"];
        NSMutableArray *arr=[[NSMutableArray alloc]init];
        [arr addObject:@"1"];
         [arr addObject:@"1"];
         [arr addObject:@"1"];
         [arr addObject:@"1"];
         [arr addObject:@"1"];
         [arr addObject:@"1"];
        [dic setObject:arr forKey:@"list"];
        arr=nil;
        [_dataList addObject:dic];
        dic=nil;
        
    }
    
    self.table_View.sectionFooterHeight = 0;
    self.table_View.sectionHeaderHeight = 0;
    self.isOpen = NO;
    
    
}




#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_dataList count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.isOpen) {
        if (self.selectIndex.section == section) {
            return [[[_dataList objectAtIndex:section] objectForKey:@"list"] count]+1;;
        }
    }
    return 1;
}
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        return 40;
    }
    return 60;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isOpen&&self.selectIndex.section == indexPath.section&&indexPath.row!=0) {

        
        static NSString *CellIdentifier = @"SignsViewCell";
        
        SignsViewCell *cell = (SignsViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell==nil){
            
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SignsViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
           
        }
        NSLog(@"indexPath.row=%d",indexPath.row);
        switch (indexPath.row) {
            case 1:
            {
                cell.nameLabel.text=@"意识";
                cell.contentLabel.text=@"清醒";
                cell.unitLabel.text=@"";
                cell.unitLabel.frame=CGRectMake(205, 19, 45, 21);
            }
                break;
            case 2:
            {
                cell.nameLabel.text=@"体温";
                cell.contentLabel.text=@"36.5";
                cell.unitLabel.text=@"℃";
                cell.unitLabel.frame=CGRectMake(200, 19, 45, 21);
            }
                break;
            case 3:
            {
                cell.nameLabel.text=@"脉搏";
                cell.contentLabel.text=@"120";
                cell.unitLabel.text=@"次/分";
                cell.unitLabel.frame=CGRectMake(197, 19, 45, 21);
            }
                break;
            case 4:
            {
                cell.nameLabel.text=@"呼吸";
                cell.contentLabel.text=@"60";
                cell.unitLabel.text=@"次/分";
                cell.unitLabel.frame=CGRectMake(185, 19, 45, 21);
             
            }
                break;
            case 5:
            {
                
                cell.nameLabel.text=@"血压";
                cell.contentLabel.text=@"120";
                cell.unitLabel.text=@"mmhg";
                cell.unitLabel.frame=CGRectMake(195, 19, 45, 21);
            }
                break;
            case 6:
            {
                cell.nameLabel.text=@"血氧饱和度";
                cell.contentLabel.text=@"30";
                cell.unitLabel.text=@"%";
                cell.unitLabel.frame=CGRectMake(185, 19, 45, 21);
                
            }
                break;
                
            default:
            {
                cell.unitLabel.text=@"";
                cell.unitLabel.frame=CGRectMake(195, 19, 45, 21);
            }
                break;
        }
        
        
        
        
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
        
        
    }else
    {
        static NSString *CellIdentifier = @"Cell1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
         NSLog(@"++++indexPath.row=%d",indexPath.row);
        
        //cell.backgroundColor=[UIColor grayColor];
        NSString *name = [[_dataList objectAtIndex:indexPath.section] objectForKey:@"name"];
        cell.textLabel.text = name;
       
        //[cell changeArrowWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
        return cell;
    }
}


#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        if ([indexPath isEqual:self.selectIndex]) {
            self.isOpen = NO;
            [self didSelectCellRowFirstDo:NO nextDo:NO];
            self.selectIndex = nil;
            
        }else
        {
            if (!self.selectIndex) {
                self.selectIndex = indexPath;
                [self didSelectCellRowFirstDo:YES nextDo:NO];
                
            }else
            {
                
                [self didSelectCellRowFirstDo:NO nextDo:YES];
            }
        }
        
    }else
    {
        NSDictionary *dic = [_dataList objectAtIndex:indexPath.section];
        NSArray *list = [dic objectForKey:@"list"];
        NSString *item = [list objectAtIndex:indexPath.row-1];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:item message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil] ;
        [alert show];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (void)didSelectCellRowFirstDo:(BOOL)firstDoInsert nextDo:(BOOL)nextDoInsert
{
    self.isOpen = firstDoInsert;
    
    //    UITableViewCell *cell = [self.expansionTableView cellForRowAtIndexPath:self.selectIndex];
    //    [cell changeArrowWithUp:firstDoInsert];
    
    [self.table_View beginUpdates];
    
    int section = self.selectIndex.section;
    int contentCount = [[[_dataList objectAtIndex:section] objectForKey:@"list"] count];
	NSMutableArray* rowToInsert = [[NSMutableArray alloc] init];
	for (NSUInteger i = 1; i < contentCount + 1; i++) {
		NSIndexPath* indexPathToInsert = [NSIndexPath indexPathForRow:i inSection:section];
		[rowToInsert addObject:indexPathToInsert];
	}
	
	if (firstDoInsert)
    {   [self.table_View insertRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop];
    }
	else
    {
        [self.table_View deleteRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop];
    }
    
	rowToInsert=nil;
	
	[self.table_View endUpdates];
    if (nextDoInsert) {
        self.isOpen = YES;
        self.selectIndex = [self.table_View indexPathForSelectedRow];
        [self didSelectCellRowFirstDo:YES nextDo:NO];
    }
    if (self.isOpen) [self.table_View scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];
}






-(void)leftBar_clicked:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    
}


-(void)rightBar_clicked:(id)sender{
    
    
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
